<?php
/**
 * Add a new Proffessional Experience
 *
 * @author Xavier VILAIN
 */
use Hive\Contact\Interview\Status;
use Hive\Ressource\Interview;
use Fox\Core\Errors;
use Fox\Generator;
use Hive\Ressource;
use Fox\Popup;
use Fox\Html\Screen;

$xview = Xview::instance();
$rid = $xview->val('rid');
$iid = $xview->val('iid');
/* @var $contact Hive\Ressource */
$contact = $xview->component('ressource', 'object');
$interview = $contact->findInterviews($iid.':'.$rid);
$data = $interview->getData();
$screen = new Screen('sInterview','update',null,true,Generator::ajaxReload('contentCol',
			Generator::url('ressource','interviews',null,array('rid' => $rid))));

$block = $xview->component('ressource','interview',array('rid' => $rid));


if($interview->status()->type == Status::TYPE_CLOSE) {
	$block->item('bInterviewP1')->collapsed(true);
	$block->item('bInterviewP2')->collapsed(false);
	$block->item('bInterviewP3')->collapsed(false);
}
$screen->add($block);

$screen->initialize($data);

if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		// If Status has not changed
		if($fields['interview_status'] == $interview->interview_status) {
			// If current status is not a closing status and conclusion text has been set
			
			if(!empty($fields['text3']) && $interview->status()->type != Status::TYPE_CLOSE) {
				// Force the status to be a closen one 
				//@TODO has their could be more than one closen status
			}
		}

		try {
			$interview->setFields($fields);
			$contact->replaceInterviews($interview);
			$contact->prepareUpdate(Ressource::CHANGE_INTERVIEWS);
			if($contact->update()) {
				Fox\Message::Success(Msg(Hive::BUNDLE,Hive::MSG_INTERVIEW_UPDATED));
			} else {
				Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_NO_UPDATE,
									Msg(Hive::BUNDLE,Hive::TXT_INTERVIEW)));
			}
		}
		catch (Fox\Object\Exception\Save $e)
		{
			Fox\Message::Error($e);
		}
		catch (Fox\Object\Exception\NotExist $e)
		{
			Fox\Message::Error($e);
		}
		$xview->messages(false,true);
		exit;
	}
}
$popup = new Popup('add',Msg(Hive::BUNDLE,Hive::MSG_UPDATE_INTERVIEW),$screen,800);
echo $popup->display();